package cn.joeg.dp.singleton.lazy;

/**
 * Created by CQ on 2018/3/7.
 */

//懒汉式单例类
//双重锁单例
public class LazySingleton3 {

    //1.第一步将构造方法私有化
    private LazySingleton3() {

    }

    //2.然后声明一个静态变量保存单例的引用
    private static volatile LazySingleton3 lazy = null;

    //方法不会再被阻塞，大大提高了程序执行性能
    public static LazySingleton3 getInstance() {
        if (lazy == null) {
            // 只要用了 synchronized关键字 就好会阻塞
            synchronized (LazySingleton3.class) {
                if (lazy == null) {
                    lazy = new LazySingleton3();
                }
            }
//            LazySingleton3 s = new LazySingleton3();
//            System.out.println(System.currentTimeMillis() + "," + lazy);
//            System.out.println(s);
//            lazy = s;
        }
        return lazy;
    }


    public void sayHello() {
        System.out.println("Hello!" + this.hashCode());
    }

}
